package com.dandelion.manager.system.web.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dandelion.commons.annotation.SystemLog;
import com.dandelion.commons.constants.BaseConst;
import com.dandelion.commons.spring.SpringContextHolder;
import com.dandelion.commons.utils.StringUtils;
import com.dandelion.commons.web.controller.BaseController;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.commons.web.model.DataGrid;
import com.dandelion.manager.system.domain.Role;
import com.dandelion.manager.system.domain.RoleResource;
import com.dandelion.manager.system.service.RoleResourceService;
import com.dandelion.manager.system.service.RoleService;

import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateNotFoundException;

@Controller
@RequestMapping("/manager/system/role")
public class RoleController extends BaseController {
	
	@Autowired
	public RoleService service ;
	
	@Autowired
	public RoleResourceService rrService ;

	@Autowired
	public RoleResourceService roleResourceService ;
	
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	/**
	 * 表单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	/**
	 * 添加
	 * @param dto
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[角色管理]", method="添加角色")
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse add(Role dto) {
		return this.service.add(dto);
	}
	
	/**
	 * 删除
	 * @param ids 数组
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[角色管理]", method="删除角色")
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse delete(Long[] ids) {
		return this.service.deleteBatch(ids);
	}
	
	/**
	 * 修改
	 * @param dto
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[角色管理]", method="修改角色")
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse update(Role dto) {
		return this.service.update(dto);
	}
	
	/**
	 * 根据ID获取数据
	 * @param id
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[角色管理]", method="加载角色")
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public @ResponseBody Role get(@PathVariable Long id) {
		return this.service.get(id) ;
	}
	
	/**
	 * 数据列表-分页查询
	 * @param dto
	 * @return
	 * @throws IOException 
	 * @throws ParseException 
	 * @throws MalformedTemplateNameException 
	 * @throws TemplateNotFoundException 
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[角色管理]", method="加载角色列表")
	@RequestMapping(value="/datagrid")
	public @ResponseBody DataGrid<Role> datagrid(Role dto){
		return this.service.datagrid(dto) ;
	}
	
	/**
	 * 查询所有
	 * @param dto
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[角色管理]", method="加载所有角色")
	@RequestMapping(value="/getAllList")
	public @ResponseBody List<Role> getAllList(Role dto) {
		return this.service.getAllList(dto) ;
	}
	
	/**
	 * 添加资源权限到角色页面
	 * @param roleId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/role_resource/{roleId}")
	public String role_resource(@PathVariable String roleId, Model model) {
		if(StringUtils.isNotBlank(roleId)) {
			model.addAttribute("roleId", roleId);
		}
		return viewName("role_resource");
	}
	
	/**
	 * 添加资源权限
	 * @param roleId
	 * @param resourceIds
	 * @param permitIds
	 * @return
	 */
	@SystemLog(module=BaseConst.MODULE_SYSTEM+"[角色管理]", method="添加资源到角色")
	@RequestMapping(value="/role_resource_add")
	public @ResponseBody AjaxResponse role_resource_add(Long roleId, Long[] resourceIds, String permitIds) {
		this.roleResourceService.addOrUpdateRoleResource(roleId, resourceIds, permitIds);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}
	
	/**
	 * 加载角色所拥有的资源权限
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value="/loadRoleByResPermitList/{roleId}")
	public @ResponseBody List<RoleResource> loadRoleByResPermitList(@PathVariable Long roleId) {
		return this.rrService.selectRoleResourceByRole(roleId) ;
	}
	
	/**
	 * 加载角色所拥有的资源权限
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value="/selectRoleIn/{roleIds}")
	public @ResponseBody List<Role> selectRoleIn(@PathVariable String roleIds) {
		return this.service.selectRoleIn(roleIds) ;
	}
	
}
